package com.mf.all.result;

import com.mf.all.emuns.ResultCodeEnum;
import com.mf.all.exception.ApiException;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

/**
 * @Author: lmf
 * @Create: 2025/3/8 20:29
 * Description:  返回结果
 */
@Data
@Schema(description = "返回结果")
public class Result<T> {
    @Schema(description = "返回码")
    private Integer code;
    @Schema(description = "返回信息")
    private String message;
    @Schema(description = "返回数据")
    private T data;
    @Schema(description = "返回时间戳")
    private long timestamp;

    public Result() {
        this.timestamp = System.currentTimeMillis();
    }

    public static <T> Result<T> success(T data) {
        final Result<T> result = setSuccess();
        result.setData(data);
        return result;
    }

    public static <T> Result<T> success(){
        return setSuccess();
    }

    private static <T> Result<T> setSuccess(){
        final Result<T> result = new Result<>();
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        return result;
    }

    public static <T> Result<T> fail(ApiException resultCodeEnum){
        final Result<T> result = new Result<>();
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        result.setData(null);
        return  result;
    }

    public static <T> Result<T> fail(String message){
        final Result<T> result = new Result<>();
        result.setCode(ResultCodeEnum.ERROR.getCode());
        result.setMessage(message);
        result.setData(null);
        return  result;
    }

}
